package geyue.wechat.robot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.blade.kit.StringKit;

import geyue.wechat.Constant;

public class TuringRobot implements Robot{
	public static void main(String[] args) {
		TuringRobot tr=new TuringRobot();
		System.out.println(tr.talk("hello"));
	}
	private  String APIKEY ;
	public TuringRobot() {
		//接入机器人，输入问题
         APIKEY = "7f8ed767531247c28419bf51533182a9";	
	}
	@Override
	public String talk(String msg) {
		String INFO="";
		 StringBuffer sb=null;
		try {
			INFO = URLEncoder.encode(msg, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}//这里可以输入问题
        String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
        URL getUrl=null;
		try {
			getUrl = new URL(getURL);
			   HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
		        connection.connect();

		        BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
		        sb = new StringBuffer();
		        String line = "";		        
					while ((line = reader.readLine()) != null) {
					    sb.append(line);
					}
				
		        reader.close();
		        // 断开连接
		        connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
			
	    String[] ss = new String[10];
	    if (sb==null) return "";
		String s = sb.toString();
        String answer;
        ss = s.split(":");
        answer = ss[ss.length-1];
        answer = answer.substring(1,answer.length()-2);
        return answer;
	}
}
